﻿using FRMS.BUS;
using FRMS.DAL;
using FRMS.Model;
using Microsoft.AspNetCore.Http;
using Microsoft.AspNetCore.Mvc;
using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Threading.Tasks;
using System.Text;
using Microsoft.AspNetCore.Authorization;

namespace FRMS.Controllers
{
    [Route("api/[controller]")]
    [ApiController]
    [Authorize]
    public class DeviceNonstandardController : ControllerBase
    {

        /// <summary>
        /// 控制接口-梯控模式下发
        /// </summary>
        /// <param name="JLdata"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LiftModeCMD")]
        public ResponseMessage LiftModeCMD(JLNonstandard JLdata)
        {
            ResponseMessage message = new ResponseMessage();
            JLNonstandardData CustomData = new JLNonstandardData();
            ModbusRtu mrtu = new ModbusRtu();
            try
            {
                JLNonstandardResponse JLNonstandard = new JLNonstandardResponse();
                JLNonstandard.Name = "customRequest";
                JLNonstandard.UUID = JLdata.UUID;
                JLNonstandard.TimeStamp = 111111;
                JLNonstandard.Session = JLdata.Session;
                CustomData.CustomData = mrtu.GetLiftStateCmd(JLdata.workmode);
                JLNonstandard.Data = CustomData;
                LogHelper.Log("切换梯控模式" + JsonConvert.SerializeObject(JLNonstandard));
                string re = HttpClientPost("http://" + JLdata.DeviceIp + ":8011/Request", JLNonstandard);
                JLNonstandardReturn JLNonstandardReturn = JsonConvert.DeserializeObject<JLNonstandardReturn>(re);
                if (JLNonstandardReturn.Code == 1)
                {
                    message.errcode = 0;
                    message.data = "";
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = JLNonstandardReturn.Message;
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "梯控模式下发";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
            }
            return message;
        }

        /// <summary>
        /// 控制接口-显示内容下发
        /// </summary>
        /// <param name="JLdata"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("LiftDisplaymsg")]
        public ResponseMessage LiftDisplaymsg(JLDisplaymsg JLdata)
        {
            ResponseMessage message = new ResponseMessage();
            JLDisplaymsgData Displaymsg = new JLDisplaymsgData();
            JLDisplaymsgData mrtu = new JLDisplaymsgData();
            try
            {
                JLDisplaymsgResponse JLDisplaymsg = new JLDisplaymsgResponse();
                JLDisplaymsg.Name = "customRequest";
                JLDisplaymsg.UUID = JLdata.UUID;
                JLDisplaymsg.TimeStamp = 111111;
                JLDisplaymsg.Session = JLdata.Session;
                Displaymsg.DisplaymsgData = JLdata.DisplaymsgData;
                mrtu.DisplaymsgData = JLdata.DisplaymsgData;
                JLDisplaymsg.Data = mrtu;
                string re = HttpClientPost("http://" + JLdata.DeviceIp + ":8011/Request", JLDisplaymsg);
                JLNonstandardReturn JLNonstandardReturn = JsonConvert.DeserializeObject<JLNonstandardReturn>(re);
                if (JLNonstandardReturn.Code == 1)
                {
                    message.errcode = 0;
                    message.data = "";
                }
                else
                {
                    message.errcode = 1;
                    message.errmsg = JLNonstandardReturn.Message;
                }
            }
            catch (Exception e)
            {
                DataClearBUS dataClearBUS = new DataClearBUS();
                ErrorLog errorLog = new ErrorLog();
                errorLog.ErrorMsg = e.ToString();
                errorLog.Operation = "显示内容下发";
                dataClearBUS.AddErrorLog(errorLog);
                message.errcode = 1;
            }
            return message;
        }

        /// <summary>
        /// 485数据接收接口
        /// </summary>
        /// <param name="JLdata"></param>
        /// <returns></returns>
        [HttpPost]
        [Route("comdataRequest")]
        public void comdataRequest(JLCommsgResponse JLCommsg)
        {
            //string logprint = CommVars.Configuration.GetSection("LogPrint").Value;
            string logprint = AppSettingsHelper.Configuration["LogPrint"];
            if (logprint == "1")
            {
                LogHelper.Log(JsonConvert.SerializeObject(JLCommsg));
            }
            //串口数据
            ApplyJLBUS ApplyJLBUS = new ApplyJLBUS();
            ApplyJLBUS.comdataRequest(JLCommsg);
        }

        //public static string ByteToBinary(string s)
        //{
        //    byte[] data = Encoding.Unicode.GetBytes(s);
        //    StringBuilder result = new StringBuilder(data.Length * 8);
        //    foreach (byte b in data)
        //    {
        //        result.Append(Convert.ToString(b, 2).PadLeft(8, '0'));
        //    }
        //    return result.ToString();
        //}
        public string HttpClientPost(string url, object datajson)
        {
            HttpClient httpClient = new HttpClient();//http对象
            //表头参数
            httpClient.DefaultRequestHeaders.Accept.Clear();
            httpClient.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));

            //转为链接需要的格式
            HttpContent httpContent = new JsonContent(datajson);
            //请求
            HttpResponseMessage response = httpClient.PostAsync(url, httpContent).Result;
            if (response.IsSuccessStatusCode)
            {
                Task<string> t = response.Content.ReadAsStringAsync();
                if (t != null)
                {
                    return t.Result;
                }
            }
            return "";
        }
    }
}
